package com.itboye.sunsun.adapter;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.alipay.android.phone.mrpc.core.ac;
import com.itboye.sunsunhome.R;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.LunTanHomePageBean.LunTanItemBean;
import com.itboye.sunsun.constants.NetPublicConstant;
import com.itboye.sunsun.luntan.LunTanDetailActivity;
import com.itboye.sunsun.utils.DebugToast;
import com.itboye.sunsun.utils.XImageLoader;
import com.pobing.uilibs.extend.component.dialog.ImageViewerDialog;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class LunTanDetailAdapter extends BaseAdapter {

	List<LunTanItemBean> dataList;
	Activity activity;

	public LunTanDetailAdapter(List<LunTanItemBean> dataList, Activity activity) {
		// TODO Auto-generated constructor stub
		this.dataList = dataList;
		this.activity = activity;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return dataList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(App.ctx).inflate(
					R.layout.item_lun_detail, null);
			holder = new ViewHolder();
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.content = (TextView) convertView.findViewById(R.id.content);
			holder.time = (TextView) convertView.findViewById(R.id.time);
			holder.zan = (TextView) convertView.findViewById(R.id.zan);
			holder.commont = (TextView) convertView.findViewById(R.id.commont);

			holder.head = (ImageView) convertView.findViewById(R.id.head);
			holder.iv1 = (ImageView) convertView.findViewById(R.id.iv1);
			holder.iv2 = (ImageView) convertView.findViewById(R.id.iv2);
			holder.iv3 = (ImageView) convertView.findViewById(R.id.iv3);
			holder.jing = (ImageView) convertView.findViewById(R.id.jing);
holder.zhiding=(ImageView) convertView.findViewById(R.id.zhiding);
			holder.iv1.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					List<String> imgs = dataList.get((int) v.getTag()).getImg();
					if (imgs != null && imgs.size() > 0) {
						ImageViewerDialog imageViewerDialog = new ImageViewerDialog(
								activity);
						String urls[] = new String[imgs.size()];
						for (int i = 0; i < urls.length; i++) {
							urls[i] = NetPublicConstant.IMAGE_URL + imgs.get(i);
						}
						imageViewerDialog.setImageUrls(urls);
						imageViewerDialog.show(0);
					}

				}
			});
			holder.iv2.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					List<String> imgs = dataList.get((int) v.getTag()).getImg();
					if (imgs != null && imgs.size() > 1) {
						ImageViewerDialog imageViewerDialog = new ImageViewerDialog(
								activity);
						String urls[] = new String[imgs.size()];
						for (int i = 0; i < urls.length; i++) {
							urls[i] = NetPublicConstant.IMAGE_URL + imgs.get(i);
						}
						imageViewerDialog.setImageUrls(urls);
						imageViewerDialog.show(1);
					}

				}
			});
			holder.iv3.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					List<String> imgs = dataList.get((int) v.getTag()).getImg();
					if (imgs != null && imgs.size() > 2) {
						ImageViewerDialog imageViewerDialog = new ImageViewerDialog(
								activity);
						String urls[] = new String[imgs.size()];
						for (int i = 0; i < urls.length; i++) {
							urls[i] = NetPublicConstant.IMAGE_URL + imgs.get(i);
						}
						imageViewerDialog.setImageUrls(urls);
						imageViewerDialog.show(2);
					}

				}
			});
			holder.imgsContainer = (ViewGroup) convertView
					.findViewById(R.id.imgsContainer);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		LunTanItemBean bean = dataList.get(position);

		if (bean.getUserInfo() == null) {
			holder.name.setText(bean.getAuthor());
		} else {
			holder.name.setText(bean.getUserInfo().getNickname());
		}

		// 如果不是精品贴
		if (dataList.get(position).getDigest().equals("0")) {
			holder.jing.setVisibility(View.INVISIBLE);
		} else if (dataList.get(position).getDigest().equals("1")) {
			holder.jing.setVisibility(View.VISIBLE);
		}
		
		if(dataList.get(position).getStick().equals("0"))
		{
			holder.zhiding.setVisibility(View.INVISIBLE);
		}else{
			holder.zhiding.setVisibility(View.VISIBLE);
		}
		// Toast.makeText(App.ctx, dataList.get(position).getDigest(),
		// 0).show();

		holder.content.setText(bean.getSubject());

		holder.time.setText(bean.getDatelineShow());
		holder.zan.setText(bean.getLikes());
		holder.commont.setText(bean.getReplies());

		List<String> imgs = bean.getImg();
		holder.iv1.setImageBitmap(null);
		holder.iv2.setImageBitmap(null);
		holder.iv3.setImageBitmap(null);
		holder.iv1.setTag(position);
		holder.iv2.setTag(position);
		holder.iv3.setTag(position);

		if (imgs != null && imgs.size() > 0) {
			holder.imgsContainer.setVisibility(View.VISIBLE);
			if (imgs.size() > 0) {
				XImageLoader.load(NetPublicConstant.IMAGE_URL + imgs.get(0),
						holder.iv1);
			}

			if (imgs.size() > 1) {
				XImageLoader.load(NetPublicConstant.IMAGE_URL + imgs.get(1),
						holder.iv2);
			}

			if (imgs.size() > 2) {
				XImageLoader.load(NetPublicConstant.IMAGE_URL + imgs.get(2),
						holder.iv3);
			}
		} else {
			holder.imgsContainer.setVisibility(View.GONE);
		}

		holder.head.setImageBitmap(null);
		try {
			if (bean.getUserInfo().getHead() == ""
					|| bean.getUserInfo().getHead() == null) {
				Bitmap bitmap = BitmapFactory.decodeResource(
						activity.getResources(), R.drawable.defaultimage);

				holder.head.setImageBitmap(bitmap);
			} else {
				holder.head.setImageBitmap(null);
				if (!bean.getUserInfo().getHead().startsWith("http")) {
					XImageLoader.load(NetPublicConstant.IMAGE_URL
							+ bean.getUserInfo().getHead(), holder.head);
				} else {
					XImageLoader
							.load(bean.getUserInfo().getHead(), holder.head);
				}
			}
		} catch (Exception e) {
		}
		return convertView;
	}

	class ViewHolder {

		TextView name;
		TextView content;
		TextView time;
		TextView zan;
		TextView commont;
		ImageView head;
		ImageView zhiding;
		ImageView iv1;
		ImageView iv2;
		ImageView iv3;
		ImageView jing;

		ViewGroup imgsContainer;
	}
}
